﻿ #include <iostream>

using namespace std;

#define  DELETE(pointer)  if(pointer){delete pointer; pointer=nullptr;}

enum EProductName
{
    PRODUCT_FROM = 0,
    APPLE = PRODUCT_FROM,
    BANANA = 1,
    PRODUCT_TO
};

class IProduct
{
public:
    virtual void Show() = 0;
};

class Apple:public IProduct
{
public:
    Apple() {};
    virtual ~Apple() {};
    void Show() { cout<< "Apple" << endl; };
};

class Banana:public IProduct
{
public:
    Banana() {};
    virtual ~Banana() {};
    void Show() { cout<< "Banana" << endl; };
};


class IFacory
{
public:
    virtual IProduct* CreateProduct(EProductName name) = 0;
};

class SimpleFactory:public IFacory
{
public:
    SimpleFactory(){}
    virtual ~SimpleFactory(){}

    IProduct* CreateProduct(EProductName name) override
    {
        if (name == APPLE)
        {
            return new Apple;
        }
        else if (name == BANANA)
        {
            return new Banana;
        }
        else
        {
            return nullptr;
        }
    }
};

int createDemo(EProductName name)
{
    IFacory* factory = new SimpleFactory;
    if (factory == nullptr)
    {
        printf("factory is null");
        return -1;
    }

    IProduct* product = factory->CreateProduct(name);
    if (product == nullptr)
    {
        printf("product is null");

        DELETE(factory);
        return -1;
    }

    product->Show();

    DELETE(factory);
    DELETE(product);

}
int main()
{
    createDemo(APPLE);
    createDemo(BANANA);

    return 0;
}
